home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / speaker_lib.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  5KB  |  165 lines

  1. //////////////////////////////////////////////////////////////////////
  2. // File - speaker_lib.c
  3. //
  4. // Library for accessing the speaker directly on the motherboard.
  5. // Code was generated by WinDriver Wizard.
  6. // Application uses WinDriver to access the hardware.
  7. // 
  8. //////////////////////////////////////////////////////////////////////
  9.  
  10. #include "speaker_lib.h"
  11. #include <stdio.h>
  12.  
  13. // this string is set to an error message, if one occurs
  14. CHAR SPEAKER_ErrorString[1024];
  15.  
  16. // internal function used by SPEAKER_Open()
  17. void SPEAKER_SetCardElements(SPEAKER_HANDLE hSPEAKER);
  18.  
  19. BOOL SPEAKER_Open (SPEAKER_HANDLE *phSPEAKER)
  20. {
  21.     SPEAKER_HANDLE hSPEAKER = (SPEAKER_HANDLE) malloc (sizeof (SPEAKER_STRUCT));
  22.  
  23.     WD_VERSION ver;
  24.  
  25.     *phSPEAKER = NULL;
  26.     SPEAKER_ErrorString[0] = '\0';
  27.     BZERO(*hSPEAKER);
  28.  
  29.     hSPEAKER->hWD = WD_Open();
  30.  
  31.     // check if handle valid & version OK
  32.     if (hSPEAKER->hWD==INVALID_HANDLE_VALUE)
  33.     {
  34.         sprintf( SPEAKER_ErrorString, "Failed opening " WD_PROD_NAME " device\n");
  35.         goto Exit;
  36.     }
  37.  
  38.     BZERO(ver);
  39.     WD_Version(hSPEAKER->hWD,&ver);
  40.     if (ver.dwVer<WD_VER)
  41.     {
  42.         sprintf( SPEAKER_ErrorString, "Incorrect " WD_PROD_NAME " version\n");
  43.         goto Exit;
  44.     }
  45.  
  46.     SPEAKER_SetCardElements(hSPEAKER);
  47.     hSPEAKER->cardReg.fCheckLockOnly = FALSE;
  48.     WD_CardRegister (hSPEAKER->hWD, &hSPEAKER->cardReg);
  49.     if (hSPEAKER->cardReg.hCard==0)
  50.     {
  51.         sprintf ( SPEAKER_ErrorString, "Failed locking device\n");
  52.         goto Exit;
  53.     }
  54.  
  55.     // Open finished OK
  56.     *phSPEAKER = hSPEAKER;
  57.     return TRUE;
  58.  
  59. Exit:
  60.     // Error during Open
  61.     if (hSPEAKER->cardReg.hCard) 
  62.         WD_CardUnregister(hSPEAKER->hWD, &hSPEAKER->cardReg);
  63.     if (hSPEAKER->hWD!=INVALID_HANDLE_VALUE)
  64.         WD_Close(hSPEAKER->hWD);
  65.     free (hSPEAKER);
  66.     return FALSE;
  67. }
  68.  
  69. void SPEAKER_Close(SPEAKER_HANDLE hSPEAKER)
  70. {
  71.     // unregister card
  72.     if (hSPEAKER->cardReg.hCard) 
  73.         WD_CardUnregister(hSPEAKER->hWD, &hSPEAKER->cardReg);
  74.  
  75.     // close WinDriver
  76.     WD_Close(hSPEAKER->hWD);
  77.  
  78.     free (hSPEAKER);
  79. }
  80.  
  81. void SPEAKER_SetCardElements(SPEAKER_HANDLE hSPEAKER)
  82. {
  83.     int i=0;
  84.     WD_ITEMS* pItem;
  85.     hSPEAKER->cardReg.Card.dwItems = SPEAKER_ITEMS;
  86.     pItem = &hSPEAKER->cardReg.Card.Item[0];
  87.  
  88.     // SPEAKER IO range
  89.     pItem[SPEAKER_IO_42].item = ITEM_IO;
  90.     pItem[SPEAKER_IO_42].fNotSharable = FALSE;
  91.     pItem[SPEAKER_IO_42].I.IO.dwAddr = SPEAKER_IO_ADDR42;
  92.     pItem[SPEAKER_IO_42].I.IO.dwBytes = 1;
  93.     pItem[SPEAKER_IO_43].item = ITEM_IO;
  94.     pItem[SPEAKER_IO_43].fNotSharable = FALSE;
  95.     pItem[SPEAKER_IO_43].I.IO.dwAddr = SPEAKER_IO_ADDR43;
  96.     pItem[SPEAKER_IO_43].I.IO.dwBytes = 1;
  97.     pItem[SPEAKER_IO_61].item = ITEM_IO;
  98.     pItem[SPEAKER_IO_61].fNotSharable = FALSE;
  99.     pItem[SPEAKER_IO_61].I.IO.dwAddr = SPEAKER_IO_ADDR61;
  100.     pItem[SPEAKER_IO_61].I.IO.dwBytes = 1;
  101. }
  102.  
  103. void SPEAKER_WriteCtrl (SPEAKER_HANDLE hSPEAKER, BYTE data)
  104. {
  105.     WD_TRANSFER trans;
  106.  
  107.     BZERO(trans);
  108.     trans.cmdTrans = WP_BYTE;
  109.     trans.dwPort = SPEAKER_IO_ADDR61;
  110.     trans.Data.Byte = data;
  111.     WD_Transfer(hSPEAKER->hWD, &trans);
  112. }
  113.  
  114. BYTE SPEAKER_ReadCtrl (SPEAKER_HANDLE hSPEAKER)
  115. {
  116.     WD_TRANSFER trans;
  117.  
  118.     BZERO(trans);
  119.     trans.cmdTrans = RP_BYTE;
  120.     trans.dwPort = SPEAKER_IO_ADDR61;
  121.     WD_Transfer(hSPEAKER->hWD, &trans);
  122.     return trans.Data.Byte;
  123. }
  124.  
  125. void SPEAKER_WriteTimerData (SPEAKER_HANDLE hSPEAKER, BYTE data)
  126. {
  127.     WD_TRANSFER trans;
  128.  
  129.     BZERO(trans);
  130.     trans.cmdTrans = WP_BYTE;
  131.     trans.dwPort = SPEAKER_IO_ADDR42;
  132.     trans.Data.Byte = data;
  133.     WD_Transfer(hSPEAKER->hWD, &trans);
  134. }
  135.  
  136. void SPEAKER_WriteTimerCtrl (SPEAKER_HANDLE hSPEAKER, BYTE data)
  137. {
  138.     WD_TRANSFER trans;
  139.  
  140.     BZERO(trans);
  141.     trans.cmdTrans = WP_BYTE;
  142.     trans.dwPort = SPEAKER_IO_ADDR43;
  143.     trans.Data.Byte = data;
  144.     WD_Transfer(hSPEAKER->hWD, &trans);
  145. }
  146.  
  147. void SPEAKER_Tone (SPEAKER_HANDLE hSPEAKER, DWORD dwHz, DWORD dwMilli)
  148. {
  149.     DWORD dwDevisor = 1190000 / dwHz;
  150.     BYTE bCtrl;
  151.     WD_SLEEP sleep;
  152.     
  153.     SPEAKER_WriteTimerCtrl(hSPEAKER, 0xb6);
  154.     SPEAKER_WriteTimerData(hSPEAKER, (BYTE) (dwDevisor & 0xff));
  155.     SPEAKER_WriteTimerData(hSPEAKER, (BYTE) ((dwDevisor >> 8) & 0xff));
  156.  
  157.     bCtrl = SPEAKER_ReadCtrl(hSPEAKER);
  158.     SPEAKER_WriteCtrl(hSPEAKER, (BYTE) (bCtrl | (BIT0 | BIT1)));
  159.        BZERO(sleep);
  160.     sleep.dwMicroSeconds = dwMilli * 1000 ; // 2 secounds
  161.     WD_Sleep(hSPEAKER->hWD, &sleep);
  162.     SPEAKER_WriteCtrl(hSPEAKER, (BYTE) (bCtrl & ~(BIT0 | BIT1)));
  163. }
  164.  
  165.